/*
  Copyright (c) 2008-2013  John Lee (j.y.lee@yeah.net)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

  * Neither the name of the copyright holders nor the names of
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_HPP
#error "Include <look.h> or <look.hpp> instead of this file."
#else   // __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_HPP

#include <type_traits>

namespace look {
namespace helper {
template <typename> class sched_t;
template <template <typename, int...> class Sched, typename T, int... Ns>
class sched_t <Sched <T, Ns...>> {
public:
  [[gnu::always_inline]] inline sched_t () { }
  [[gnu::always_inline]] inline void init ()
  {
    __asm__(
      ".global %c[sched]"   "\n\t"
      ".weak %c[sched]"     "\n\t"
      ".set %c[sched],%c[s]"
      : : [sched] "i" (&scheduler),
          [s] "i" (static_cast <Sched <T, Ns...>*> (this))
    );
  }
};

template <typename> class intr_t;
template <template <typename, int...> class Intr, typename T, int... Vectors>
class intr_t <Intr <T, Vectors...>> {
public:
  [[gnu::always_inline]] inline intr_t () { }

protected:
  [[gnu::always_inline]] inline void init ()
  {
    auto p = static_cast <Intr <T, Vectors...>*> (this);
    helper_t <void, Vectors...>::make (p);
  }

private:
  template <typename U, int N, int... Ns>
  struct helper_t {
    [[gnu::always_inline]] inline static void make (Intr <T, Vectors...>* v)
    {
      helper_t <U, N>::make (v);
      helper_t <U, Ns...>::make (v);
    }
  };

  template <typename U, int N>
  struct helper_t <U, N> {
    static_assert (N >= -1, "the Vector must be greater or equal than -1");
    [[gnu::always_inline]] inline static void make (Intr <T, Vectors...>* v)
    { interrupt_t::attach <N> (v); }
  };
};

template <typename> class systick_t;
template <template <typename, int...> class Intr, typename T>
class systick_t <Intr <T>> : public intr_t <Intr <T, -1>> {
public:
  [[gnu::always_inline]] inline systick_t () { }

protected:
  [[gnu::always_inline]] inline void init ()
  {
    intr_t <Intr <T, -1>>::init ();
    __asm__(
      ".global %c[systick]"       "\n\t"
      ".weak %c[systick]"         "\n\t"
      ".set %c[systick],%c[s]"
      : : [systick] "i" (&systick), [s] "i" (static_cast <Intr <T>*> (this))
    );
  }
};

template <typename> class task_t;
template <template <typename, int...> class Task, typename T, int StkSz>
class task_t <Task <T, StkSz>> {
public:
  [[gnu::always_inline]] inline task_t () { }

protected:
  [[gnu::always_inline]] inline void init ()
  {
    auto* p = static_cast <T*> (static_cast <Task <T, StkSz>*> (this));
    p->init (stack[(StkSz + 1) & ~1], reinterpret_cast <void*> (&T::routine));
    __asm__(
      ".global .LOOK.STACK.%c[t]"     "\n\t"
      ".weak .LOOK.STACK.%c[t]"       "\n\t"
      ".set .LOOK.STACK.%c[t],%c[s]"
     : : [t] "i" (p), [s] "i" (sizeof (stack) / sizeof (stack[0]))
    );
  }

private:
  [[gnu::aligned(8)]] uintptr_t stack[((StkSz + 1) & ~1) + T::MIN_CONTEXT_SIZE];
};

template <typename> class idle_task_t;
template <template <typename, int...> class Task, typename T, int StkSz>
class idle_task_t <Task <T, StkSz>> : public task_t <Task <T, StkSz>> {
public:
  [[gnu::always_inline]] inline idle_task_t () { }

protected:
  [[gnu::always_inline]] inline void init ()
  {
    task_t <Task <T, StkSz>>::init ();
    __asm__(
      ".global %c[idle]"    "\n\t"
      ".weak %c[idle]"      "\n\t"
      ".set %c[idle],%c[t]"
      : : [idle] "i" (&idle_task),
          [t] "i" (static_cast <Task <T, StkSz>*> (this))
    );
  }
};

template <template <typename, int...> class Task, typename T>
class idle_task_t <Task <T>>
: public idle_task_t <Task <T, LOOK_IDLE_STACK_SIZE>> { };

template <typename> class user_task_t;
template <template <typename, int...> class Task, typename T, int StkSz>
class user_task_t <Task <T, StkSz>> : public task_t <Task <T, StkSz>> {
public:
  [[gnu::always_inline]] inline user_task_t () { }
  [[gnu::always_inline]] inline void init ()
  {
    task_t <Task <T, StkSz>>::init ();
    scheduler.create (*static_cast <Task <T, StkSz>*> (this));
  }
};

template <template <typename, int...> class Task, typename T>
class user_task_t <Task <T>>
: public user_task_t <Task <T, LOOK_STACK_SIZE>> { };

template <typename> class invalid_t;

template <template <typename, int...> class T, typename U, int... Ns>
using instance_t = typename std::conditional <
  std::is_base_of <look::sched_t, U>::value,
  sched_t <T <U, Ns...>>,
  typename std::conditional <
    std::is_base_of <look::interrupt_t, U>::value,
    typename std::conditional <
      std::is_base_of <look::systick_t, U>::value,
      systick_t <T <U>>,
      intr_t <T <U, Ns...>>
    >::type,
    typename std::conditional <
      std::is_base_of <look::idle_task_t, U>::value,
      typename std::conditional <
        std::is_base_of <look::task_t, U>::value,
        user_task_t <T <U, Ns...>>,
        idle_task_t <T <U, Ns...>>
      >::type,
      invalid_t <T <U, Ns...>>
    >::type
  >::type
>::type;
}

template <typename T, int... Ns>
struct instance : T, helper::instance_t <instance, T, Ns...> {
  template <typename... R>
  [[gnu::always_inline]] inline instance (R&&... r)
  : T (r...) { helper::instance_t <instance, T, Ns...>::init (); }
};
}
#endif  // __LIGHTWEIGHT_OBJECT_ORENTED_KERNEL_HPP
